package com.cljz.makepassenger.present.activity;

import com.cljz.makepassenger.base.BaseP;
import com.cljz.makepassenger.model.net.api.Api;
import com.cljz.makepassenger.model.net.bean.ResponseBean;
import com.cljz.makepassenger.ui.activity.ExchangeActivity;
import com.cljz.makepassenger.utils.L;

import java.util.HashMap;

import cn.droidlover.xdroidmvp.net.ApiSubcriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;
import cn.droidlover.xdroidmvp.net.diy.Base64Utils;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by CLJZ on 2017/6/7.
 */

public class ExChangeP extends BaseP<ExchangeActivity> {

    public void exchangePoint(String number) {
        HashMap<String, String> map = new HashMap<>();
        map.put("token", getToken());
        map.put("points_num", number);
        String body = Base64Utils.enCode(gson.toJson(map));
        final RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().exchangePoint(requestBody)
                .compose(XApi.<ResponseBean>getApiTransformer())
                .compose(XApi.<ResponseBean>getScheduler())
                .compose(getV().<ResponseBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ResponseBean>() {
                    @Override
                    protected void onFail(NetError error) {
                        L.e(error.toString());
                        getV().exchangeFailed(error.toString());
                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {

                        if ("0".equals(responseBean.getError_code())) {
                            getV().exchangeSuccess();
                        } else if ("1000".equals(responseBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().exchangeFailed(responseBean.getMsg());
                        }
                    }
                });

    }
}
